var express = require('express');
var router = express.Router();
var mongoose= require('mongoose');
var Post = mongoose.model('Post');

module.exports = router;

//标签列表页
router.get('/', function (req, res) {
  Post.getTags(function (err, tags) {
    if(err){
      req.flash('error',err);
      return res.render('error',{message:'查询标签列表失败',error:err});
    }
    console.log('tags:',tags);

    res.render('tags',{
      title:'标签',
      tags:tags,
      user:req.session.user,
      success:req.flash('success').toString(),
      error:req.flash('error').toString()
    });
  });
});

//指定标签下的博客
router.get('/:tag', function (req, res) {
  var p=req.query.p?parseInt(req.query.p):1;
  var limit=2;
  Post.getPage({tags:{$in:[req.params.tag]}},p,limit,function(err,posts,total){
    if(err){
      req.flash('error','查询此标签下的博客失败');
      return res.render('error',{
        message:'查询此标签下的博客失败',
        error:err
      });
    }
    var pageCnt=Math.ceil(total/limit);
    res.render('tag',{
      title:'TAG:'+req.params.tag,
      user:req.session.user,
      posts:posts,
      page:{
        curr:p,
        total:pageCnt,
        path:'/tags/'+req.params.tag+'?',
        first:'p=1',
        prev:'p='+(p-1),
        next:'p='+(p+1),
        last:'p='+pageCnt,
        isFirstPage:p==1,
        isLastPage:(p-1)*limit+posts.length>=total
      },
      success:req.flash('success').toString(),
      error:req.flash('error').toString()
    });
  });
});